import 'package:dailoz/dio/http_util.dart';
import 'package:dailoz/response_json_entity.dart';
import 'package:dailoz/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class TagService {
  static Future<List<dynamic>> getTagList() async {
    final prefs = await SharedPreferences.getInstance();
    List<dynamic> tagList = [];
    try {
      var response = await HttpUtil().get(
        '/tag',
        data: {"id": prefs.getString("user-id")},
      );
      ResponseJsonEntity responseJsonEntity =
      ResponseJsonEntity.fromJson(response);
      if (responseJsonEntity.data != null) {
        tagList = responseJsonEntity.data;
      }
      return tagList;
    } catch (e) {
      return tagList;
    }
  }

  static void createTag(BuildContext context, dynamic provider,
      TextEditingController? controller) async {
    if (controller!.text == "") {
      showToast("标签名不能为空");
      return;
    }
    bool flag = await provider
        .addTag(controller.text);
    if (flag) {
      showToast("添加成功");
      // ignore: use_build_context_synchronously
      Navigator.pop(context);
    }
  }
}
